package vn.fpt.snake.view;

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.Timer;
import javax.swing.JPanel;
import vn.fpt.snake.ui.IView;
import vn.fpt.snake.commons.Constants;
import vn.fpt.snake.controller.GameControllerImpl;
import vn.fpt.snake.entity.Settings;

public class Board extends JPanel implements ActionListener {
	/**
	 * 
	 */
	
	private static final long serialVersionUID = 1L;
	
	GameControllerImpl gameController;
	public IView _view;
	public Timer timer;
	public int width;
	public int height;
	
	public Board(JFrame frame) throws Exception{	
		try{
			//Initial variable
			init();	
			//Set window dimension
			//Switch startup screen
			//Start game loop
			Settings settings = gameController.getSettings();
			width = settings.getWidth();
			height = settings.getHeight();
			frame.setSize(width, height);
			frame.setIconImage(gameController.getImage(Constants.SNAKE_IMG));
			gameController.switchStartupView(this);
			timer = new Timer(settings.getSpeed(), this);
			timer.start();
		}catch (Exception e) {
			// TODO: handle exception
			throw e;
		}
	}
	
	private void init(){
		try{	
			gameController = new GameControllerImpl();
			setFocusable(true);
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println(e.getMessage());
		}
	}
	
	@Override
	public void paint(Graphics g){
		// TODO Auto-generated method stub
		try{
			_view.onPaint(g);	
		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}

	@Override
	public void actionPerformed(ActionEvent arg) {
		// TODO Auto-generated method stub
			repaint();	
	}
}
